<!--
 * @Author: your name
 * @Date: 2022-01-18 11:00:21
 * @LastEditTime: 2022-01-18 11:37:46
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \算法测试\day0906\539-最小时间差.html
-->
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        /**
给定一个 24 小时制（小时:分钟 "HH:MM"）的时间列表，找出列表中任意两个时间的最小时间差并以分钟数表示。

 

示例 1：

输入：timePoints = ["23:59","00:00"]
输出：1
示例 2：

输入：timePoints = ["00:00","23:59","00:00"]
输出：0
 

提示：

2 <= timePoints <= 2 * 104
timePoints[i] 格式为 "HH:MM"

思路：sort排序 因为是字符串 然后比较相邻的时间差 考虑首尾；
 */
        var findMinDifference = function (timePoints) {
            timePoints.sort();
            let result = Number.MAX_VALUE;
            for (let i = 0; i < timePoints.length; i++) {
                let time, left, right;
                if (i + 1 === timePoints.length) {
                    left = timePoints[0].split(':')[0] * 60 + parseInt(timePoints[0].split(":")[1]);
                    right = timePoints[i].split(':')[0] * 60 + parseInt(timePoints[i].split(":")[1]);
                } else {
                    left = timePoints[i].split(':')[0] * 60 + parseInt(timePoints[i].split(":")[1]);
                    right = timePoints[i + 1].split(':')[0] * 60 + parseInt(timePoints[i + 1].split(":")[1]);
                }
                time = ChangeTime(left, right);
                if (time < result) result = time;
            }
            return result;
        };
        function ChangeTime(min, secondmin) {
            if ((secondmin - min) <= (24 * 60) / 2) {
                return secondmin - min;
            } else {
                return min + (24 * 60) - secondmin;
            }
        }
        console.log('01'*60);
    </script>
</body>

</html>